document.addEventListener('DOMContentLoaded', function () {
    const passwordInput = document.getElementById('password');
    const confirmPasswordInput = document.getElementById('confirm-password');
    const registrationForm = document.getElementById('registration-form');
    const checkbox = document.getElementById('agree-term');
    registrationForm.addEventListener('submit', function (event) {
        let password = passwordInput.value;
        let confirmPassword = confirmPasswordInput.value;
        if (password.length < 8) {
            event.preventDefault(); // 阻止表单提交
            cocoMessage.error("请检查密码是否符合要求或者两次输入是否一致",2000);
        } else if (password.length > 16) {
            cocoMessage.error("密码长度过长！",2000);
        } else if(!checkbox.checked){
            event.preventDefault();
            cocoMessage.error('请勾选用户协议');
        }
        else if(password !== confirmPassword){
            event.preventDefault();
            cocoMessage.error('两次密码输入不一致');
        }
        else {
            register();
        }
    });
    passwordInput.addEventListener('input', function () {
        const password = passwordInput.value;
        const passwordIndicator = document.getElementById("passwordIndicator");
        const passwordError = document.getElementById('passwordError');
        if (password.length >= 8) {
            passwordIndicator.style.display = 'inline';
            passwordError.style.display = 'none';
        } else if (password.length > 0 && password.length < 8) {
            passwordIndicator.style.display = 'none';
            passwordError.style.display = 'inline';
        }
    })
    confirmPasswordInput.addEventListener('input', function () {
        const confirmPassword = confirmPasswordInput.value;
        const passwordIndicator1 = document.getElementById("passwordIndicator1");
        const passwordError1 = document.getElementById('passwordError1');
        if (confirmPassword.length >= 8) {
            passwordIndicator1.style.display = 'inline';
            passwordError1.style.display = 'none';
        } else if (confirmPassword.length > 0 && confirmPassword.length < 8) {
            passwordIndicator1.style.display = 'none';
            passwordError1.style.display = 'inline';
        }
    })
});

function checkPhoneNumber(phoneNumber) {
    // 使用正则表达式检查手机号
    const phoneNumberRegex = /^1[3-9][0-9]{9}$/;
    return phoneNumberRegex.test(phoneNumber);
}

function startCountdown() {
    let countdown = 60; // 初始倒计时时间
    // 获取验证码按钮
    const getVerificationCodeButton = document.getElementById("sendVerificationCode");
    const username = document.getElementById('username');
    if (checkPhoneNumber(username.value)) {
        $.ajax({
            url: "/smsC/sendSmsCode?phoneNumber=" + username.value + "&bizType=" + "RESET_SMS_CODE",
            type: "post",
            async: false,
            cache: false,
            success: function (res) {
                if (res.code == 0) {
                    // 禁用按钮
                    getVerificationCodeButton.disabled = true;

                    // 更新按钮文本和倒计时
                    function updateButton() {
                        if (countdown <= 0) {
                            // 倒计时结束，启用按钮
                            getVerificationCodeButton.textContent = "获取验证码";
                            getVerificationCodeButton.disabled = false;
                        } else {
                            // 更新按钮文本和倒计时
                            getVerificationCodeButton.textContent = countdown + "秒后重新获取";
                            countdown--;
                            setTimeout(updateButton, 1000);
                        }
                    }
                    updateButton();
                } else {
                    alert(res.msg);
                }
            },
            error: function () {
                cocoMessage.error("验证码发送失败",2000);
            }
        })

    } else {
        cocoMessage.error("手机号格式错误",2000);
    }
}

function passwordReset() {
    $.ajax({
        type: "POST",//方法类型
        dataType: "json",//预期服务器返回的数据类型
        url: "/loginC/passwordReset",//要响应的url
        data: form2JsonString('forget-form'),
        async: false,   //false为同步，true为异步
        processData: false,
        contentType : "application/json;charset=UTF-8",
        success: function (data) {
            //表示注册不成功，由于控制器返回的是map，所以就可以用data[key]
            if (data.code != 0) {
                cocoMessage.error(data.msg);
            } else {
                cocoMessage.success("密码重置成功,为您跳转登陆页")
                setTimeout(function () {
                    window.location.href = "http://localhost:8080/userLogin.jsp";
                }, 2000);
            }
        },
        error: function (result) {
            alert("出现异常");
            //刷新窗体
            window.location.href = window.location.href;
        }
    });
}

/** 表单序列化成json字符串的方法  */
function form2JsonString(formId) {
    var paramArray = $('#' + formId).serializeArray();
    /*请求参数转json对象*/
    var jsonObj = {};
    $(paramArray).each(function () {
        jsonObj[this.name] = this.value;
    });
    console.log(jsonObj);
    // json对象再转换成json字符串
    return JSON.stringify(jsonObj);
}


